import json
import time
from bot.enterprise_wechat_gpt.qw_bot import total_process, total_process_test
from bot.enterprise_wechat_gpt.test_case.utter_agent_case import dialogue_text1
from bot.enterprise_wechat_gpt.util.merge_conversation_util import *
from bot.enterprise_wechat_gpt.util.thread_local_util import local_data

if __name__ == '__main__':
    local_test_end2end = False

    if not local_test_end2end:
        history = parse_history(dialogue_text1)
        while True:
            user_input = ''
            if history[-1].get(USER, None) is None:
                user_input = input(USER + ': ')
                history.append({USER: user_input})
            else:
                user_input = history[-1][USER]

            res_total = total_process(history, user_input, local_data.user_id)
            history.append({ASSISTANT: res_total.get("最终话术", '转人')})

    else:
        res_coll = load_test_data()
        print(len(res_coll))

        with open('./test_case.json', 'w') as fw:
            for idx in range(99, len(res_coll)):
                print('------------------{}-------------------{}'.format(idx, time.ctime()))
                history = res_coll[idx]
                print(history)
                user_input = ''
                if history[-1].get(USER, None) is None:
                    user_input = input(USER + ': ')
                    history.append({USER: user_input})
                else:
                    user_input = history[-1][USER]

                res_total = total_process_test(history, user_input)
                res_total['idx'] = idx

                res_total_jl = json.dumps(res_total, ensure_ascii=False)
                fw.write(res_total_jl)
                fw.write('\n')
                print('=================={}==================={}'.format(idx, time.ctime()))
                time.sleep(65)
